<?php


namespace app\system\controller;

class MemberApp extends AdminBase
{
	public $project = [];
	public function __construct()
	{
		parent::__construct();
		if (!defined("no_authentication")) {
			$app = \think\Db::name("app")->where(["id" => $this->input["appid"]])->find();
			if (empty($app)) {
				$this->response(\app\system\model\Code::fail, "appid不存在");
			}
			$this->project = $app;
			if ($this->project["type"] !== 1) {
				$this->response(\app\system\model\Code::fail, "该应用ID的应用类型不支持请求当前模块接口");
			}
		}
	}
	public function get_user_list()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\MemberAppService::get_user_list($options);
		if ($result["code"] != \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"]);
		}
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function get_user_info()
	{
		$options = $this->request->get();
		$result = \app\system\service\MemberAppService::get_user_info($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function add_user()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "account" => "require", "password" => "require"], ["appid.require" => "appid 不能为空", "account.require" => "账号 不能为空", "password.require" => "密码 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\MemberAppService::add_user($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function edit_user()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "id" => "require"], ["appid.require" => "appid 不能为空", "id.require" => "id 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\MemberAppService::edit_user($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del_user()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::del_user($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function user_disable()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::user_disable($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "设置成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_card_list()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\MemberAppService::get_card_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function get_card_batch_list()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\MemberAppService::get_card_batch_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]], ["all_card_data", $result["all_card_data"]]);
	}
	public function add_card()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "num" => "require|number|>=:1", "type" => "require"], ["appid.require" => "appid 不能为空", "num.require" => "请输入数量", "type.require" => "请选择类型"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		if ($options["type"] === 0 && !isset($options["time_type"])) {
			$this->response(\app\system\model\Code::fail, "请选择时长类型");
		}
		if ($options["type"] === 1 && empty($options["value"])) {
			$this->response(\app\system\model\Code::fail, "请输入面值");
		}
		$result = \app\system\service\MemberAppService::add_card($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del_card()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::del_card($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del_card_batch()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::del_card_batch($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function export_card()
	{
		// 关闭错误显示，防止 PHPExcel 的 Deprecated 警告污染 JSON 响应
		error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);

		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "batch_id" => "require", "export_file_type" => "require|in:txt,excel"], ["appid.require" => "appid 不能为空", "batch_id.require" => "batch_id 不能为空", "export_file_type.require" => "export_file_type 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$options["limit"] = $this->page_size_max;
		$options["page"] = 1;
		$result = \app\system\service\MemberAppService::export_member_card_table($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"], $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_user_level_list()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\MemberAppService::get_user_level_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function get_user_level_info()
	{
		$options = $this->request->get();
		$result = \app\system\service\MemberAppService::get_user_level_info($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function add_user_level()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["appid" => "require", "name" => "require", "level" => "require|number", "integral" => "require|number", "upgrade" => "require|number", "is_default" => "require|number"], ["appid.require" => "appid 不能为空", "name.require" => "等级名称 不能为空", "level.require" => "级别号 不能为空", "integral.require" => "所需成长值 不能为空", "upgrade.require" => "用户是否可升级 不能为空", "is_default.require" => "是否默认等级 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\MemberAppService::add_user_level($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function edit_user_level()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require", "appid" => "require", "name" => "require", "level" => "require|number", "integral" => "require|number", "upgrade" => "require|number", "is_default" => "require|number"], ["id.require" => "等级id 不能为空", "appid.require" => "appid 不能为空", "name.require" => "等级名称 不能为空", "level.require" => "级别号 不能为空", "integral.require" => "所需成长值 不能为空", "upgrade.require" => "用户是否可升级 不能为空", "is_default.require" => "是否默认等级 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\MemberAppService::edit_user_level($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "编辑成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del_user_level()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::del_user_level($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function user_level_move_new_level()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::user_level_move_new_level($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "转移成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_user_account_finance_log()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\MemberAppService::get_user_account_finance_log($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function get_user_unbinding_log()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\MemberAppService::get_user_unbinding_log($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function condition_force_offline_user()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::condition_force_offline_user($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function user_force_offline()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::user_force_offline($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], "设置成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function user_force_offline_machine()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::user_force_offline_machine($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], "设置成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function condition_del_user()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::condition_del_user($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function condition_unbinding_user()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::condition_unbinding_user($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function condition_increase_duration_user()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::condition_increase_duration_user($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function condition_increase_point_user()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::condition_increase_point_user($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function condition_disable_user()
	{
		$options = $this->request->get();
		$validate = \think\Validate::make(["appid" => "require", "value" => "require"], ["appid.require" => "appid 不能为空", "value.require" => "value 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\MemberAppService::condition_disable_user($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function get_login_machine_list()
	{
		$options = $this->request->get();
		$validate = \think\Validate::make(["appid" => "require", "id" => "require"], ["appid.require" => "appid 不能为空", "id.require" => "id 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\MemberAppService::get_login_machine_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function del_login_machine()
	{
		$options = $this->request->post();
		$result = \app\system\service\MemberAppService::del_login_machine($options);
		if ($result["code"] == \app\system\model\Code::success) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
}